package edu.nf.service.impl;


import edu.nf.SQLExecutor;
import edu.nf.beans.annotations.Component;
import edu.nf.beans.annotations.Inject;
import edu.nf.dao.FindEmpByIdDao;
import edu.nf.dao.UpdateEmpDao;
import edu.nf.dto.EmpAndDepartmDto;
import edu.nf.entity.Departm;
import edu.nf.entity.Emp;
import edu.nf.exception.UpdateException;
import edu.nf.service.UpdateEmpService;
import edu.nf.utils.SQLExecutorFactory;

@Component("UpdateEmpService")
public class UpdateEmpServiceImpl implements UpdateEmpService {

    @Inject(beanName = "findEmpById")
    private FindEmpByIdDao findEmpByIdDao;


    @Inject(beanName = "updateEmpDao")
    private UpdateEmpDao empDao;

    @Override
    public void updateEmpInfo(EmpAndDepartmDto dto) {

        String departmId = findEmpByIdDao.findEmpByEmpId(dto.getEmpId()).getDepartmId();
        Emp e = new Emp();
        e.setEmpid(dto.getEmpId());
        e.setEmpname(dto.getEmpname());
        e.setSex(dto.getSex());
        e.setPay(dto.getPay());
        e.setDepartmId(dto.getDepartmId());


        Departm d = new Departm();
        d.setDepartmId(dto.getDepartmId());
        d.setName(dto.getDepartmName());

        SQLExecutor se = SQLExecutorFactory.getSQLExecutor();
        se.beginTransaction();
        try{

            empDao.updateEmp(e);
            se.commit();
        }catch (RuntimeException e){
            se.rollback();
            throw new UpdateException("update fail");
        }
    }
}
